import { AterFormatRichText } from './text';
import { NBorder, NFill, NFont } from '../../../types/style.interface';


export function textRender(width: number, height: number, font: NFont.IFont, letterSpace: number = 1, scale: number = 1) {
    let getStartY = (texts: AterFormatRichText[][]) => {
        let lineHeight = (font.lineHeight || 2) * scale;
        let allHeight = (texts.length - 1) * lineHeight;
        
        for(let rowTexts of texts) {
            if(rowTexts.length === 0) {
                allHeight += (font.size || 12) * scale;
                continue;
            }
            let max = 0;
            for(let textItem of rowTexts) {
                // @ts-ignore
                if(textItem.size.h > max) max = textItem.size.h;
            }
            allHeight += max;
        }
        if(allHeight >= height || font.vertical === NFont.EVertical.top) {
            console.log('1', allHeight <= height,  font.vertical === NFont.EVertical.top)
            return 0;
        }
        if(font.vertical === NFont.EVertical.middle) {
            console.log('2')
            return (height - allHeight) / 2;
        }
        if(font.vertical === NFont.EVertical.bottom) {
            console.log('3')
            return height - allHeight;
        }
    };
    const getStartX = (font: Pick<NFont.IFont, 'horizontally'>) => {
        let getTotalWidth = (_texts: AterFormatRichText[]) => {
            let widths = 0;
            for(let text of _texts) {
                widths += text.width;
            }
            let latterSpaces = _texts.length > 0 ? (_texts.length - 1) * letterSpace : 0
            return widths + latterSpaces;
        }
        let _getStartX: (texts: AterFormatRichText[]) => number = () => {
            return 0;
        }
        if(font.horizontally === NFont.EHorizontally.center) {
            _getStartX = (texts: AterFormatRichText[]) => {
                let totalWidth = getTotalWidth(texts);
                if(totalWidth > width) {
                    return 0
                }
                return (width - totalWidth) / 2;
            }
        }
        if(font.horizontally === NFont.EHorizontally.right) {
            _getStartX = (texts: AterFormatRichText[]) => {
                let totalWidth = getTotalWidth(texts);
                if(totalWidth > width) {
                    return 0;
                }
                return width - totalWidth;
            }
        }
        return _getStartX;
    }
    return (texts: AterFormatRichText[][]) => {
        let result: {y: number, texts: ({x: number}&AterFormatRichText)[]}[] = [];
        let startY = getStartY(texts);
        let pastEndY = startY;
        for(let i = 0; i < texts.length; i++) {
            let rowTexts: {y: number, texts: ({x: number}&AterFormatRichText)[]} = {y: pastEndY, texts: []};
            let currentX = getStartX(font)(texts[i]); 
            for(let j = 0; j < texts[i].length; j++) {
                let text = {
                    ...texts[i][j],
                    x: currentX
                }
                rowTexts.texts.push(text);
                currentX += texts[i][j].width + letterSpace;
            }
            result.push(rowTexts);
        }
        return result;
    }
}